<template>
	<view class="">
		<u-action-sheet class="sheet" @close="closeSheet" :closeOnClickOverlay="true" round="15"
			:safeAreaInsetBottom="true" :show="isSheet">
			<view class="gunabi" @click="closeSheet" ><image src="https://img-cdn.duobashuzi.com/xianggui/img/gunabi.png" mode="scaleToFill"></image></view>
			<view class="top_sheet">
				
				<view class="ser_type">
					<serve-add @specId="handleCustomEvent" :isCart.sync="isCart" :goodsItem.sync="goodsInfo" :goodsSpec.sync="specList"></serve-add>
				</view>

			</view>
			<view class="total-box">
				
				<view class="t_r" v-if="isCart == 0">
					<view class="bottom_barright">
						<view class="add_cart" @click="addCart">
							加入购物车
						</view>
						<view class="right_buy" @click="addOrder">
							立即购买
						</view>
					</view>
				</view>
				<view class="t_r" v-else>
					<u-button v-if="isCart==1" class="addOrder" :loading="load" text="加入购物车" shape="circle" @click="addCart"
						color="#ff8855"></u-button>
					<u-button v-else-if="isCart==2" class="addOrder" :loading="load" text="立即购买" shape="circle" @click="addOrder"
						color="#ff4444"></u-button>
						
					<u-button v-else class="addOrder" :loading="load" text="确认" shape="circle" @click="queren"
						color="#ff4444"></u-button>
				</view>
				
			</view>
		</u-action-sheet>
	</view>
</template>

<script>
	import serveAdd from "@/components/serves/serveAdd.vue"
	import {addShopCart,buyNow,goodsSpecList,updateCartSku} from "@/api/index.js"
	import {checkGoodsStatus} from '@/api/addOrder.js';
	export default {
		name: "sheetBottom",
		props: {
			isSheet: {
				type: Boolean,
				default: false
			},
			goodsInfo: {
				type: Object
			},
			cartId:{
				type:Number
			},
			number:{
				type:Number
			},
			isCart:{
				type:Number,
				default:0
			}
		},
		components: {
			serveAdd
		},
		data() {
			return {
				load:false,
				specId:'',
				specList:'',
				itemTex:'',
				marketPrice:''
			}
		},
		watch: {
			"goodsInfo"(newVal, old) {
				this.goodsInfo = newVal;
				console.log(newVal)
				console.log(old)
			}
		},
		mounted() {
		  this.$nextTick(() => {
		    console.log("数据已更新");
		  });
		},
		methods: {
			//获取规格列表
			getgoodsSpecList(){
				console.log("触发")
				console.log(this.goodsInfo)
				console.log(this.cartId)
				console.log(this.specId)
				
				if(this.specId == ''){
					console.log(this.goodsInfo.id)
					goodsSpecList({goods_id:this.goodsInfo.id}).then(res=>{
						console.log(res)
						
						let data = res.result;
						Object.keys(data).forEach(key => {
						  data[key].forEach(item => {
							item.selected = false;
						  });
						});
						this.specList = data;
					}).catch(err=>{
						console.log(err)
						uni.showToast({
							title:err.message,
							icon:'none'
						})
					})
				}
			},
			
			handleCustomEvent(data) {
			  // 处理从子组件接收到的数据
			  console.log('接收到的数据:', data);
			  this.specId = data.item_id; // 更新父组件的状态
			  this.itemTex = data.itemTex;
			  this.pricelist = data.price_list;
			},
			closeSheet() {
				console.log('触发111')
				this.$emit("closeSheet", false)
				this.$emit('itemTex',this.itemTex)
				this.$emit('pricelist',this.pricelist)
				
				this.total = 0
				console.log(this.techInfo)
			},
			keepTwoDecimalFull(num) {
				var result = parseFloat(num);
				if (isNaN(result)) {
					return false;
				}
				result = Math.round(num * 100) / 100;
				var s_x = result.toString(); //将数字转换为字符串
				var pos_decimal = s_x.indexOf('.'); //小数点的索引值
				// 当整数时，pos_decimal=-1 自动补0  
				if (pos_decimal < 0) {
					pos_decimal = s_x.length;
					s_x += '.';
				}
				// 当数字的长度< 小数点索引+2时，补0  
				while (s_x.length <= pos_decimal + 2) {
					s_x += '0';
				}
				return s_x;
			},
			async addOrder(){
				console.log(this.goodsInfo)
				const goods = this.goodsInfo
				const arr = []
				// const addInfo = {
				// 	openid:uni.getStorageSync("openid"),
				// 	goods_id:this.goodsInfo.goods_id,
				// 	goods_num:this.goodsInfo.goods_num
				// }
				// this.load = true
				 this.getOrderCarDetail()
	
			},
			//确认
			queren(){
				// 
				console.log(this.cartId)
				const goods = this.goodsInfo;
				let sku_id ='';
				if(this.goodsInfo.is_spec == 1){
					if(this.specId == ''){
						uni.showToast({
							title:'请选择规格',
							icon:'none'
						});
						return false;
					}
					sku_id = this.specId
				}else{
					sku_id = 0
				}
				const addInfo = {
					cart_id:this.cartId,
					sku_id:sku_id
				}
				console.log(addInfo)
				updateCartSku(addInfo).then(res=>{
					console.log(res)
				
					uni.showToast({
						icon:'none',
						title:'已成功更换规格'
					})
					this.$emit("update:isSheet",false)
					this.$emit('itemTex',this.itemTex)
					this.$emit("goodsId",this.goodsInfo)
					this.$emit('pricelist',this.pricelist)
				}).catch(err=>{
					
					uni.showToast({
						title:err.message,
						icon:'none'
					})
				})
			},
			
			//加入购物车
			addCart(){
				const goods = this.goodsInfo;
			    const aa =  this.$refs.serveAdd;
				let sku_id ='';
				if(this.goodsInfo.is_spec == 1){
					if(this.specId == ''){
						uni.showToast({
							title:'请选择规格',
							icon:'none'
						});
						return false;
					}
					sku_id = this.specId
				}else{
					sku_id = 0
				}
				const arr = []
				const addInfo = {
					product_id:this.goodsInfo.id,
					number:this.goodsInfo.minimum_order_quantity,
					sku_id:sku_id
				}
				console.log(addInfo)
				addShopCart(addInfo).then(res=>{
					console.log(res.count)

					uni.showToast({
						icon:'none',
						title:'已成功添加到购物车'
					})
					this.$emit("update:isSheet",false)
					this.$emit('itemTex',this.itemTex)
					this.$emit('count',res.count)
					this.$emit("goodsId",this.goodsInfo)
				}).catch(err=>{
					
					uni.showToast({
						title:err.message,
						icon:'none'
					})
				})
			},
			getOrderCarDetail(){
				let sku_id ='';
				if(this.goodsInfo.is_spec == 1){
					if(this.specId == ''){
						uni.showToast({
							title:'请选择规格',
							icon:'none'
						});
						return false;
					}
					sku_id = this.specId
				}else{
					sku_id = 0
				}
				const data = {
					number:this.goodsInfo.minimum_order_quantity,
					goods_id:this.goodsInfo.id,
					sku_id:sku_id
				}
				buyNow(data).then(res=>{
					console.log(res)
					uni.navigateTo({
						url:"/package/addOrder/index?cart_ids="+res.result
					})
					// uni.navigateTo({
					// 	url:"/package/addOrder/index?order_type=2"
					// })
				}).catch(err=>{
					uni.showToast({
						title:err.msg,
						icon:'none'
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>

	/deep/.u-popup__content {
		overflow: hidden;
	}

	$myfont-color:#292929;

	.cred {
		color: #e54d42;
	}

	@mixin ellipsis {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}
	.bottom_barright{
		width: 100%;
		display: flex;
		margin-top: 100px;
		.right_buy{
			width: 50%;
			height: 80rpx;
			line-height: 80rpx;
			background: #ff4444;
			text-align: center;
			color: #fff;
			border-radius: 0 20px 20px 0;
			font-size: 14px;
		}
		.add_cart{
			width: 50%;
			height: 80rpx;
			line-height: 80rpx;
			background: #ff8855;
			color: #fff;
			text-align: center;
			border-radius:20px 0 0 20px;
			font-size: 14px;
		}
	}
	.gunabi{
		width: 16px;
		height: 16px;
		position: absolute;
		right: 10px;
		top: 10px;
		image{
			display: block;
			width: 100%;
			height: 100%;
		}
	}
	.sheet {
		background-color: #fff;
	}

	.top_sheet {
		padding: 10px;
		background-color: #fff;
		border-top-left-radius: 15px;
		border-top-right-radius: 15px;
		.card_info {
			border-radius: 10px;
			border: 1px solid #724ce6;
			padding: 10px;
			display: flex;
			background-color: #fff;

			.sr_title {
				flex: 1;
				display: flex;
				margin-left: 10px;

				.username {
					color: $myfont-color;
					font-weight: 700;
					font-size: 16px;
					max-width: 67px;
					@include ellipsis;
				}

				.score {
					padding-left: 10px;

					.icon-starmin-in-h {
						display: inline-block;
						width: 12px;
						height: 11px;

						background-size: 100%;
					}

					.score_num {
						color: #999;
						font-size: 12px;
						margin-left: 4px;
					}
				}
			}
		}

		.ser_type {
			// max-height: 560rpx;
			overflow: auto;
			margin: 12px auto;
			text-align: left;
		}

	}

	.total-box {
		padding: 4px 15px;
		// border-top: 1px solid #f2f2f2;
		display: flex;
		align-items: center;
		background-color: #fff;
		text-align: left;
		
		.t_l {
			flex: 1;
			font-size: 14px;
			color: #666;

			.amount {
				color: #e54d42;

				.money {
					font-size: 18px;
				}
			}
		}

		.t_r {
			width: 100%;
			display: flex;
			justify-content: space-between;
		}

	}
</style>
